/**
 * author      : dormi330
 * date        : 2018/6/27
 * project     : mybatis
 * description : TODO
 */

package com.deep.trade.infrastructure.mybatis.order;

import com.deep.trade.application.AppRuntimeEvn;
import com.deep.trade.domain.order.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MybatisOrderQueryRepository implements OrderQueryRepository {

    @Autowired
    private TradeOrderMapper tradeOrderMapper;

    @Autowired
    private OrderSummaryMapper orderSummaryMapper;

    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    @Autowired
    private TradeOrderSkuMapper tradeOrderSkuMapper;

    @Autowired
    private OrderPaymentMapper orderPaymentMapper;

    @Autowired
    private OrderParcelMapper orderParcelMapper;

    @Autowired
    private AppRuntimeEvn appRuntimeEvn;


    /**
     * 查询单个 TradeOrder
     */
    public TradeOrder tradeOrderOfId(String tradeOrderId) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return tradeOrderMapper.findOneById(tenantCode, tradeOrderId);
    }

    public TradeOrder fulfilTradeOrderSku(TradeOrder tradeOrder) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        List<TradeOrderSku> skuList = tradeOrderSkuMapper.listByTradeOrder(tenantCode, tradeOrder.getId());
        tradeOrder.setSkuList(skuList);
        return tradeOrder;
    }

    public TradeOrder fulfilDeliveryAddress(TradeOrder tradeOrder) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        DeliveryAddress deliveryAddress = deliveryAddressMapper.findOneByTradeOrder(tenantCode, tradeOrder.getId());
        tradeOrder.setDeliveryAddress(deliveryAddress);
        return tradeOrder;
    }

    /**
     * 查询单个 OrderSummary
     */
    public OrderSummary orderSummaryOfId(String orderSummaryId) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return orderSummaryMapper.findOneById(tenantCode, orderSummaryId);
    }

    public PageInfo<OrderSummary> orderSummaryOfMember(String memberId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String tenantCode = appRuntimeEvn.getTenantCode();
        List<OrderSummary> tradeOrderList = orderSummaryMapper.listByMember(tenantCode, memberId);
        return new PageInfo<>(tradeOrderList);
    }

    /**
     * 查询 OrderSummary 的所有 TradeOrder
     */
    public List<TradeOrder> tradeOderOfOrderSummary(String orderSummaryId) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return tradeOrderMapper.listByOrderSummary(tenantCode, orderSummaryId);
    }

    /**
     * 分页查询, 会员的所有订单
     */
    public PageInfo<TradeOrder> tradeOrderOfMember(String memberId, int pageNum, int pageSize) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        PageHelper.startPage(pageNum, pageSize);
        // Mybatis查询方法
        List<TradeOrder> tradeOrderList = tradeOrderMapper.listByMember(tenantCode, memberId);
        // 用PageInfo对结果进行包装
        return new PageInfo<>(tradeOrderList);
    }

    /**
     * 分页查询, 租户的所有订单
     */
    public PageInfo<TradeOrder> tradeOrderOfTenant(int pageNum, int pageSize) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        PageHelper.startPage(pageNum, pageSize);
        List<TradeOrder> tradeOrderList = tradeOrderMapper.listByTenant(tenantCode);
        return new PageInfo<>(tradeOrderList);
    }

    /**
     * 根据 租户 + tradeOrder , 查询订单的所属sku 列表
     */
    public List<TradeOrderSku> orderSkuOfTradeOrder(String tradeOrderId) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return tradeOrderSkuMapper.listByTradeOrder(tenantCode, tradeOrderId);
    }

    public List<TradeOrderSku> orderSkuOfTradeOrderList(List<String> tradeOrderIdList) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return tradeOrderSkuMapper.listByTradeOrderList(tenantCode, tradeOrderIdList);
    }

    /**
     * 根据 租户 + tradeOrder , 查询 TradeOrder 的 收货地址信息
     */
    public DeliveryAddress orderDeliveryOfTradeOrder(String tradeOrderId) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return deliveryAddressMapper.findOneByTradeOrder(tenantCode, tradeOrderId);
    }

    /**
     * 根据 租户 + tradeOrder , 查询 TradeOrder 的 的支付信息
     */
    public OrderPayment paymentOfTradeOrder(String tradeOrderId) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return orderPaymentMapper.findOneByTradeOrder(tenantCode, tradeOrderId);
    }

    /**
     * 根据 租户 + tradeOrderId , 查询包裹信息
     */
    public List<OrderParcel> parcelListOfOrder(String tradeOrderId) {
        String tenantCode = appRuntimeEvn.getTenantCode();
        return orderParcelMapper.listByTradeOrder(tenantCode, tradeOrderId);
    }

}
